// pages/wode/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: '',
    luck: '',
    istrue: '',

  },

  // 签到
  sign: function () {
    var that = this;
    app.token_md5();
    var istrue = that.data.istrue;
    if (istrue) {
      wx.request({
        url: app.url2 + 'api/luck_value',
        data: {
          openid: wx.getStorageSync('openid'),
          token: wx.getStorageSync('token_md5'),
          state: 1,
          type:1,
          is_check:true,
        },
        header: { 'content-type': 'application/json' },
        success: function (res) {
          console.log(res)
          if (res.data.code == 200) {
            that.setData({
              istrue: !istrue,
              luck: res.data.result.score+=that.data.luck,
            })
            wx.showToast({
              title: '签到成功',
              icon: 'success',
              duration: 2000
            })
          } 
        },
      }) 
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({ title: 'loading', mask: true })
    var that = this;
    app.token_md5();
    wx.request({
      url: app.url2 + 'api/check_sign',
      data: {
        openid: wx.getStorageSync('openid'),
        state: 1,
        token: wx.getStorageSync('token_md5'),
      },
      header: { 'content-type': 'application/json' },
      success: function(res) {
        that.setData({
          luck: (res.data.result ? res.data.result.value : 0),
          istrue: (res.data.code == 200)? true : false
        })
      },
    }) 

    wx.request({
      url: app.url2 + 'api/yue',
      data: {
        openid: wx.getStorageSync('openid'),
        token: wx.getStorageSync('token_md5'),
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        console.log(res);
        that.setData({
          user_img: wx.getStorageSync('avatarurl'),
          user_name: wx.getStorageSync('nickname'),
          money_num: res.data.money,
        })
        wx.hideLoading();                                    
      }
    })
  },
})